import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpTest {
	
	public static void main(String[] args) {
		String httpUrl = "http://apis.baidu.com/apistore/weatherservice/citylist";
		String httpArg = "cityname=杭州";
		String jsonResult = request(httpUrl, httpArg);
		jsonResult = unicode2String(jsonResult);
		System.out.println(jsonResult);
		
		//String str = new String("\u5317\u4eac","UTF8");
		
		//System.out.println("杭州");
		//String ascii="\u6d59\u6c5f";
		//System.out.println(ascii);

	}

	/**
	 * @param urlAll
	 *            :请求接口
	 * @param httpArg
	 *            :参数
	 * @return 返回结果
	 */
	public static String request(String httpUrl, String httpArg) {
	    BufferedReader reader = null;
	    String result = null;
	    StringBuffer sbf = new StringBuffer();
	    httpUrl = httpUrl + "?" + httpArg;

	    try {
	        URL url = new URL(httpUrl);
	        HttpURLConnection connection = (HttpURLConnection) url
	                .openConnection();
	        connection.setRequestMethod("GET");
	        // 填入apikey到HTTP header
	        connection.setRequestProperty("apikey",  "93c5781b105075f4b8fee203e48dfbec");
	        //connection.
	        connection.connect();
	        
	        System.out.println("编码格式：" + connection.getContentEncoding());
	        System.out.println("Content-Type:" + connection.getContentType());
	        //connection.
	        InputStream is = connection.getInputStream();
	        reader = new BufferedReader(new InputStreamReader(is, "GBK"));
	        String strRead = null;
	        while ((strRead = reader.readLine()) != null) {
	            sbf.append(strRead);
	            sbf.append("\r\n");
	        }
	        reader.close();
	        result = sbf.toString();
	        
	        
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return result;
	}
	
	public static String ascii2native(String ascii) {  
	    int n = ascii.length() / 6;  
	    StringBuilder sb = new StringBuilder(n);  
	    for (int i = 0, j = 2; i < n; i++, j += 6) {  
	        String code = ascii.substring(j, j + 4);  
	        char ch = (char) Integer.parseInt(code, 16);  
	        sb.append(ch);  
	    }  
	    return sb.toString();  
	}  
	
	
	public static String unicode2String(String unicode) {
		 
	    StringBuffer string = new StringBuffer();
	    
	    int index = 0;
	    index = unicode.indexOf("\\u");
	    while(index >= 0) {
	    	string.append(unicode.substring(0, index));
	    	String hex = unicode.substring(index + 2, index + 6);
	    	string.append((char)Integer.parseInt(hex, 16));
	    	unicode = unicode.substring(index + 6);
	    	index = unicode.indexOf("\\u");
	    }
	    string.append(unicode);
	    
	    return string.toString();
	}

	
	/**
	 * @param urlAll
	 *            :请求接口
	 * @param httpArg
	 *            :参数
	 * @return 返回结果
	 */
	public static String request2(String httpUrl, String httpArg) {
	    BufferedReader reader = null;
	    String result = null;
	    StringBuffer sbf = new StringBuffer();
	    httpUrl = httpUrl + "?" + httpArg;

	    try {
	        URL url = new URL(httpUrl);
	        HttpURLConnection connection = (HttpURLConnection) url
	                .openConnection();
	        connection.setRequestMethod("GET");
	        // 填入apikey到HTTP header
	        connection.setRequestProperty("apikey",  "93c5781b105075f4b8fee203e48dfbec");
	        connection.connect();
	        InputStream is = connection.getInputStream();
	        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
	        String strRead = null;
	        while ((strRead = reader.readLine()) != null) {
	            sbf.append(strRead);
	            sbf.append("\r\n");
	        }
	        reader.close();
	        result = sbf.toString();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return result;
	}
}
